package org.aeon.service;

import org.aeon.dao.UserDao
import org.aeon.entity.User

/**
 * @author: MissingAeon
 * @date: 2024/1/18 16:54
 * @description: 登陆
 */
class Login {
    private val userDao: UserDao = UserDao()

    fun login(): User? {
        println("请输入登录账号:")
        val username = readln()
        println("请输入密码:")
        val password = readln()
        val user = validate(username, password)
        if (user != null) {
            println("欢迎回来，尊敬的贵宾:$username")
        } else {
            println("密码错误！")
        }
        return user
    }

    private fun validate(username: String, password: String): User? {
        if (userDao.exist(username)) {
            val user = userDao.queryById(username)
            if (user?.password == password) {
                return user
            }
        }
        return null
    }

    // 退出当前账号
    fun logOut(): User? {
        println("谢谢惠顾，欢迎下次光临！")
        return null
    }

}
